#include "formdeviceprofile.h"
#include "ui_formdeviceprofile.h"

#include <QSettings>

FormDeviceProfile::FormDeviceProfile(CProfile *pProfile, QWidget *parent) :
    QWidget(parent), m_pProfile(pProfile),
    m_ui(new Ui::FormDeviceProfile)
{
    m_ui->setupUi(this);

    LoadData();
}

FormDeviceProfile::~FormDeviceProfile()
{
    SaveData();
    delete m_ui;
}

void FormDeviceProfile::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void FormDeviceProfile::Save2Profile()
{
    m_pProfile->m_nHeight = m_ui->lineEditHeight->text().toInt();
    m_pProfile->m_nWidth = m_ui->lineEditWidth->text().toInt();
    m_pProfile->m_nMaxFiles = m_ui->lineEditMaxFileNumber->text().toInt();
    m_pProfile->m_nImageQuality = m_ui->lineEditImageQuality->text().toInt();
    m_pProfile->m_nHeightPreview = m_ui->lineEditHeightPreview->text().toInt();
    m_pProfile->m_nWidthPreview = m_ui->lineEditWidthPreview->text().toInt();
    m_pProfile->n_nStartNumber = m_ui->lineEditStartNumber->text().toInt();
    m_pProfile->m_bEnablePreviewWhileProcessing = m_ui->checkBoxEnablePreview->checkState() == Qt::Checked;
}

void FormDeviceProfile::LoadData()
{
    QSettings settings;
    m_ui->lineEditHeight->setText(settings.value("deviceprofile/height", 600).toString());
    m_ui->lineEditWidth->setText(settings.value("deviceprofile/width", 800).toString());
    m_ui->lineEditMaxFileNumber->setText(settings.value("deviceprofile/maxfiles", 2000).toString());
    m_ui->lineEditImageQuality->setText(settings.value("deviceprofile/imagequal", 90).toString());
    m_ui->lineEditHeightPreview->setText(settings.value("deviceprofile/heightpreview", 600).toString());
    m_ui->lineEditWidthPreview->setText(settings.value("deviceprofile/widthpreview", 800).toString());
    m_ui->lineEditStartNumber->setText(settings.value("deviceprofile/startnumber", 1).toString());
    m_ui->checkBoxEnablePreview->setCheckState((Qt::CheckState)settings.value("deviceprofile/enablepreview", 0).toInt());

    Save2Profile();
}

void FormDeviceProfile::SaveData()
{
    QSettings settings;
    settings.setValue("deviceprofile/height", m_ui->lineEditHeight->text());
    settings.setValue("deviceprofile/width", m_ui->lineEditWidth->text());
    settings.setValue("deviceprofile/maxfiles", m_ui->lineEditMaxFileNumber->text());
    settings.setValue("deviceprofile/imagequal", m_ui->lineEditImageQuality->text());
    settings.setValue("deviceprofile/heightpreview", m_ui->lineEditHeightPreview->text());
    settings.setValue("deviceprofile/widthpreview", m_ui->lineEditWidthPreview->text());
    settings.setValue("deviceprofile/startnumber", m_ui->lineEditStartNumber->text());
    settings.setValue("deviceprofile/enablepreview", m_ui->checkBoxEnablePreview->checkState());

    Save2Profile();
}
